package com.sy.utils;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sy.exception.BilibiliException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SmsUtils {
    private static final String API_KEY = "5ce27913fdde5689a952ba1c8d55d8fb";

    /**
     * 发送单条短信的方法
     * @param phone 手机号
     * @param code 验证码
     */
    public static void sendMsg(String phone, String code) {
        HttpURLConnection connection = null;
        DataOutputStream out = null;
        BufferedReader reader = null;
        InputStreamReader isr = null;
        InputStream is = null;
        try {
            //创建连接
            URL url = new URL("http://api01.monyun.cn:7901/sms/v2/std/single_send");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();
            // POST请求
            out = new DataOutputStream(connection.getOutputStream());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("apikey", API_KEY);
            jsonObject.put("mobile", phone);
            jsonObject.put("content", URLEncoder.encode("验证码：" + code + "，打死都不要告诉别人哦！", "gbk"));
            out.writeBytes(jsonObject.toJSONString());
            out.flush();
            // 读取响应
            is = connection.getInputStream();
            isr = new InputStreamReader(is);
            reader = new BufferedReader(isr);
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                sb.append(lines);
            }
            JSONObject result = JSON.parseObject(sb.toString());

            //result打印结果为 {"result":0,"custid":"1631910831203785917","msgid":1631910831203785917,"desc":""}
            System.out.println(result);
            if (result.getInteger("result") != 0) {
                throw new BilibiliException("短信验证码发送失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BilibiliException(e);
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            if (null != connection) {
                connection.disconnect();
            }
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
            if (null != isr) {
                try {
                    isr.close();
                } catch (IOException e) {
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void main(String[] args) {
        sendMsg("17372576657","qwe1");
    }
}
